class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        occ = set()
        n = len(s)
        ans = 0
        r = -1
        for i in range(n):
            if i != 0:
                occ.remove(s[i-1])
            while r < n-1 and s[r+1] not in occ:
                occ.add(s[r+1])
                r+=1
            ans = max(ans,r+1-i)
        return ans
    def lengthOfLongestSubstring1(self, s: str) -> int:
        ans = 0
        left = 0
        char_map ={}
        for i in range(len(s)):
            char_map[s[i]] = char_map.get(s[i], 0) + 1
            while char_map[s[i]] >1:
                char_map[s[left]] = char_map.get(s[left], 0) - 1
                left+=1
            ans = max(ans,i-left+1)
        return ans

